<?php
/**
 * Created by PhpStorm.
 * User: von
 * Date: 2017/12/5
 * Time: 14:47
 */
namespace backend\controllers;

use backend\models\Country;
use backend\models\SearchPackage;
use backend\models\User;
use common\helps\pageTools;
use common\helps\profiles;
use common\models\Http;
use common\models\Responses;
use yii\data\Pagination;

class SearchPackageController extends BaseController{
    const LIMIT = 20; //每页显示个数
    /*
     * 显示资产包
     */
    public function actionIndex(){

        return $this->render('index');
    }
    /*
     * 资产包筛选
     *             'state' => $this->smallInteger()->notNull()->comment('状态'),//1无、2启动尽调、3已立项、4已评估、5已受让、6待跟进、
            'type' => $this->smallInteger()->notNull()->comment('资产类型'),//1保证类2抵押类3混合类
            'category' => $this->smallInteger()->notNull()->defaultValue(4)->comment('资产包类别'),//1 A自有资产包、2 B管理资产包、3 C共投共管资产包、4 D立项新包

     */
    public function actionSearch(){
        if (\Yii::$app->request->isGet){
            $keyWords  = trim(\Yii::$app->request->get('keyWords',''));     //关键词
            $principal = (int)\Yii::$app->request->get('principal',0);   //金额

            $category  = (int)\Yii::$app->request->get('category',0);    //资产包类别
            $packageState = (int)\Yii::$app->request->get('packageState',0); //当前状态

            $packageType = (int)\Yii::$app->request->get('packageType',0); //资产包类型
            $owner = trim(\Yii::$app->request->get('owner',''));            //上传人
            $province = (int)\Yii::$app->request->get('province',0);      //省份
            $city     = (int)\Yii::$app->request->get('city',0);          //城市
            $leader   = trim(\Yii::$app->request->get('leader',''));        //总监
            $charge   = trim(\Yii::$app->request->get('charge',''));        //负责人
            $teams   = trim(\Yii::$app->request->get('teams',''));         //团队人
            $industry = \Yii::$app->request->get('industry','');     //行业
            $order =   \Yii::$app->request->get('order','');     //排序
            $orderGroup =   \Yii::$app->request->get('orderGroup',0);     //字段排序
            $data = [];
            $data['page']           = trim(\Yii::$app->request->get('page','n1'));
            if(strpos( $data['page']  ,'n') !== false){
                $data['page']   = (int)substr($data['page'],1);
            }
            $data['limit']    = (int)\Yii::$app->request->get('limit',self::LIMIT);
            if (!empty($keyWords)){
                $data['keyWords']=$keyWords;
            }
            if (!empty($principal)){
                $data['principal']=profiles::getPackagePrice()[$principal];
            }
            if (!empty($category)){
                $data['category']=profiles::getCateGory()[$category];
            }
            if (!empty($packageState)){
                $data['packageState']=profiles::getPakcageState()[$packageState];
            }
            if (!empty($packageType)){
                $data['packageType']=profiles::getPackageType()[$packageType];
            }
            if (!empty($owner)){
                $data['owner']=$owner;
            }
            if (!empty($province)){
                $data['province']=$province;
            }
            if (!empty($city)){
                $data['city']=$city;
            }
            if (!empty($leader)){
                $data['leader']=$leader;
            }
            if (!empty($charge)){
                $data['charge']=$charge;
            }
            if (!empty($teams)){
                $data['teams']=$teams;
            }
            if (!empty($industry)){
                $data['industry']=$industry;
            }
            if (!empty($order)){
                $data['order']=$order;
            }
            if (!empty($orderGroup)){
                $data['orderGroup']=$orderGroup;
            }
            $data['count']  = SearchPackage::getPackageData($data,1,0);
            $pages = new Pagination(['totalCount' => $data['count'],'pageSize' => $data['limit']]);
            $dataPackage = SearchPackage::getPackageData($data,0,$pages);
            $dataPackage = json_decode($dataPackage,true);
            $provinceData = SearchPackage::getProvinces('pid=0');
            unset($data['page']);
            $data['page']           = trim(\Yii::$app->request->get('page','n1'));
            if(strpos( $data['page']  ,'n') !== false){
                $data['page']   = (int)substr($data['page'],1);
            }
            //配置文件
            $categoryName = profiles::getCateGory();
            $getPackageName = profiles::getPackageType();
            $getPakcageState = profiles::getPakcageState();

            //分页参数
            $pageUrl              = [];
            $pageUrl['action']   = 'searchpackage';
            $pageUrl['location'] = 'search';

            $dataUrl['keywords']=\Yii::$app->request->get('keyWords','');
            $dataUrl['principal']=\Yii::$app->request->get('principal','');
            $dataUrl['category']=\Yii::$app->request->get('category','');
            $dataUrl['packageState']=\Yii::$app->request->get('packageState','');
            $dataUrl['packageType']=\Yii::$app->request->get('packageType','');
            $dataUrl['owner']=\Yii::$app->request->get('owner','');
            $dataUrl['teams']=\Yii::$app->request->get('teams','');
            $dataUrl['leader']=\Yii::$app->request->get('leader','');
            $dataUrl['province']=\Yii::$app->request->get('province','');
            $dataUrl['city']=\Yii::$app->request->get('city','');
            $dataUrl['limit']=\Yii::$app->request->get('limit',self::LIMIT);
            $pageUrl['condition'] = '?' . http_build_query($dataUrl);
            //拼接分页字符串
//            $pageString  = pageTools::createPageString($pageUrl, $data['count'], $data['page'], $data['limit'] );
            $pageString = 0;
            $viewData = [
                'provinceData'=>$provinceData,
                'dataPackage'=>$dataPackage,
                'categoryName'=>$categoryName,
                'getPackageName'=>$getPackageName,
                'getPakcageState'=>$getPakcageState,
                'pageString'=>$pageString,
                'count'=>$data['count'],
                'pageNumOne'=>self::LIMIT,
                'pages'=>$pages,
            ];
            $viewData['userTeam']  = User::find()->where(['status'=>1])->asArray()->all();
            $viewData['userTeamLeardn']  = User::find()->where(["post"=>"BranchManager",'status'=>1])->asArray()->all();
            if (!empty($dataPackage) && $dataPackage['code']==200){
                $entry = \Yii::$app->request->get('entry','');
                if (!empty($entry) && $entry == 'sala'){
                    $viewData['postion'] = 2 ;
                    return $this->render('../search-all/index',$viewData);
                }else{
                    return $this->render('index',$viewData);
                }
            }
            return json_encode(Responses::response400('搜索错误'));
        }
    }
    /*
     * 点击省份获取城市
     */
    public static function actionAjaxCity(){
        $provinceID = (int)\Yii::$app->request->get('provinceID',1);
        $cityData     = SearchPackage::getCitys($provinceID);
        if (!empty($cityData)){
            return json_encode(Responses::response200($cityData));
        }
        return json_encode(Responses::response400());
    }
    /*
     * 联想数据--获取标题
     */
    public static function actionAjaxKeywords(){
        $title = \Yii::$app->request->get('keywods','');
        if (!empty($title)){
            $titleData = json_decode(SearchPackage::getKeywodsTitle($title),true);
            if (!empty($titleData) && $titleData['code']==200){
                return json_encode($titleData);
            }
            return json_encode(Responses::response200());
        }
        return json_encode(Responses::response200());
    }
}